package org.pointex.common.util;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;


public class RedisUtil {

	private  RedisTemplate<String, String> redisTemplate;
	
	public RedisUtil(RedisTemplate<String, String> redisTemplate){
		this.redisTemplate=redisTemplate;

	}
	/**
	 * 设置redisTemplate
	 * 
	 * @param redisTemplate
	 *            the redisTemplate to set
	 */

	public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {  
		this.redisTemplate = redisTemplate;  
	}  
	/**
	 * 获取 RedisSerializer <br>
	 * ------------------------------<br>
	 */
	protected  RedisSerializer<String> getRedisSerializer() {
		return redisTemplate.getStringSerializer();
	}
	
	
		/**
		 *添加对象
		 * @param hashtable
		 * @param exTime    key的过期时间   为0时永久保存
		 * @return
		 */
		public   boolean add_Redis(final Hashtable<String, Object> hashtable,final long exTime){
			boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
				public Boolean doInRedis(RedisConnection connection)  
						throws DataAccessException {  
					RedisSerializer<String> serializer = getRedisSerializer();  
					byte[] key  = serializer.serialize(hashtable.get("key").toString());  
					byte[] name = serializer.serialize(hashtable.get("value").toString());
					//设置key的过期时间    S单位
					if(exTime!=0)
					connection.setEx(key, exTime, key);
					
					return connection.setNX(key, name);  
				}  
			});  
			return result;  
		}
		
		/**
		 * 添加集合
		 * @param list  
		 * @param exTime  key的过期时间   为0时永久保存
		 * @return
		 */
		public  boolean add_Redis(final List<Hashtable<String, Object>> list,final long exTime){
			 Assert.notEmpty(list);
			 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 
			      public Boolean doInRedis(RedisConnection connection) 
			          throws DataAccessException { 
			        RedisSerializer<String> serializer = getRedisSerializer(); 
			        for (Hashtable<String, Object> hashtable : list) { 
			          byte[] key  = serializer.serialize(hashtable.get("key").toString()); 
			          byte[] name = serializer.serialize(hashtable.get("value").toString()); 
			          //设置key的过期时间
			          if(exTime!=0)
					  connection.setEx(key, exTime, key);
			          
			          connection.setNX(key, name); 
			        } 
			        return true; 
			      } 
			    }, false, true); 
			    return result; 
		}
		
		//删除对象，依赖key
		public  void delete_Redis(String key){
			redisTemplate.delete(key);
		}
		
		//删除集合，依赖key集合
		public  void delete_Redis(List<String> keys){
			redisTemplate.delete(keys);
		}
		
		/**
		 * 修改对象
		 * @param hashtable
		 * @param exTime	key的过期时间   为0时永久保存
		 * @return 
		 */
		public  boolean update_Redis(final Hashtable<String, Object> hashtable,final long exTime){
			String key=hashtable.get("key").toString();
			if(get_Redis(key)==null){
				throw new NullPointerException("数据行不存在, key = " + key);  
			}
			boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 
			      public Boolean doInRedis(RedisConnection connection) 
			          throws DataAccessException { 
			        RedisSerializer<String> serializer = getRedisSerializer(); 
			        byte[] key  = serializer.serialize(hashtable.get("key").toString()); 
			        byte[] name = serializer.serialize(hashtable.get("value").toString()); 
			        //设置key的过期时间
			        if(exTime!=0)
					connection.setEx(key, exTime, key);
			        connection.set(key, name); 
			        return true; 
			      } 
			    }); 
			 return result;  
		}
		
		//根据Key获取对象
		public  Hashtable<String, Object> get_Redis(final String keyId){
			Hashtable<String, Object> result= redisTemplate.execute(new RedisCallback<Hashtable<String, Object>>() { 
				 public Hashtable<String, Object> doInRedis(RedisConnection connection) 
				          throws DataAccessException { 
				        RedisSerializer<String> serializer = getRedisSerializer(); 
				        byte[] key = serializer.serialize(keyId); 
				        byte[] value = connection.get(key); 
				        if (value == null) { 
				          return null; 
				        } 
				        String values = serializer.deserialize(value);
				        Hashtable<String, Object> hashtable=new Hashtable<String, Object>();
				        hashtable.put("key", keyId);
				        hashtable.put("value", values);
				        return hashtable;
				      } 
				    }); 
				    return result;  
			}
		
		//添加Map值
		public void add_Map(HashMap<String, Object> hashmap, String key) {
			HashOperations<String, Object, Object> hash = redisTemplate
					.opsForHash();
			hash.putAll(key, hashmap);
		}
		
		//根据hashkey获取值
		public Object getMapbyKey(String key,String hashkey){
			HashOperations<String, Object, Object> hash= redisTemplate.opsForHash();
			return hash.get(key, hashkey);
		}
}
